<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>

<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
    <script type="text/javascript" src="/layui/layui.js"></script>
    <script type="text/javascript">

        layui.use(['table',"jquery","layer","form","upload"], function() {
            var upload = layui.upload;
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.jquery;
            table.render({
                //请求地址
                url: "${pageContext.request.contextPath}/selectAllLog"
                //开启分页
                , page: true
                //id
                , toolbar: "#myToolbar"
                , elem: "#myTable"
                //工具栏
                , cols: [
                    [{type: "checkbox", rowspan: 2}
                        , {title: "日志编号", field: "id", align: "center"}
                        , {title: "管理员姓名", field: "name", align: "center"}
                        , {title: "操作日期", field: "date", align: "center"  }
                        , {title: "用户IP", field: "ip", align: "center"}
                        , {title: "操作模块", field: "type", align: "center"}
                        , {title: "操作内容", field: "content"}

                    ]

                ]
            })

            //监控工具栏事件
            table.on("toolbar(myTableFilter)", function (obj) {
                switch (obj.event) {
                    case "add":
                        $("#mySubmitButton").prop("name","add");
                        $("#mySubmitButton").html("确认添加");
                        //alert("add");
                        $("#userName").val("");
                        $("#passWord").val("");
                        $("#myPhoto").prop("src","").prop("style","display:none");

                        layerIndex=layer.open({type:1,content:$("#updateForm")});
                        form.render();
                        break;
                }
            })

            //监控表格数据情况
            table.on('tool(myTableFilter)', function (obj) {
                //获取当前行的数据
                //console.log(obj.data)
                //获取点击按钮事件
                //console.log(obj.event);
                switch (obj.event) {

                    case 'edit': {
                        $("#mySubmitButton").prop("name","edit");
                        $("#mySubmitButton").html("确认修改");
                        //console.log(obj.data)
                        //打开框
                        //通过弹出层显示修改输入框
                        layerIndex = layer.open({type: 1, content: $("#updateForm")});
                        //数据回显
                        $("#id").val(obj.data.id);
                        $("#userName").val(obj.data.userName);
                       $("#passWord").val(obj.data.passWord);
                        //数据渲染
                        form.render();
                        break;
                    }
                    case "delete":
                        // alert("delete");

                        console.log(obj.data);
                        $.ajax({
                            url:"/deleteAdmin"
                            ,data:{id:obj.data.id}
                            ,type:"post"
                            ,success:function(data){
                                if (data.isOk) {
                                    layer.msg("删除成功", {icon: 6})
                                    table.reload("myTable")
                                } else {
                                    layer.msg("删除失败", {icon: 1})

                                }
                            }
                        })
                        break;
                }
            })

            form.on("submit(updateFormFilter)",function (obj){
                layer.close(layerIndex);//关闭修改弹出层
                //判断form表单点击进入的是add还是edit
                var path=$("#mySubmitButton").prop("name");
                alert(path)
                var toPath="";
              if (path=="edit"){
                  toPath="/updateAdmin"
              } else{
                  toPath="/insertAdmin"
              }
                var formData=new FormData($("#updateForm")[0]);

                $.ajax({
                    url: "${pageContext.request.contextPath}"+toPath
                    , data: obj.field
                    //使用同步提交方式
                    , type: "post"
                    , success: function (data) {
                        console.log(data.isOk)
                        if (data.isOk) {
                            layer.msg("操作成功", {icon: 6})
                            table.reload("myTable")
                        } else {
                            layer.msg("操作失败", {icon: 1})

                        }
                    }
                })
                return false;
            })

        })

    </script>

</head>
<body>
<table id="myTable" lay-filter="myTableFilter" ></table>
<%--工具栏模板--%>
<script type="text/html" id="myToolbar">

    <div class="layui-btn-container layui-inline">
        <button class="layui-btn layui-btn-lg layui-btn-danger" lay-event="add">添加</button>
    </div>

</script>
<%--这个是没一行后面对应的按钮--%>
<script type="text/html" id="myCaozuoTemplet">
    <a class="layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm" lay-event="delete">删除</a>
</script>

   <%----%>

<%--修改的表单--%>
    <form enctype="multipart/form-data" class="layui-form" id="updateForm" style="display:none;">
        <input name="id" id="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="userName" id="userName" required  lay-verify="required" placeholder="请输入上师名称" autocomplete="off" class="layui-input">
            </div>
        </div>



    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="text" name="passWord" id="passWord" required lay-verify="required" placeholder="请输入上师法号" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateFormFilter" name="edit" id="mySubmitButton">确认修改</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
   </form>

    </body>
</html>
